//
//  ENLoginViewController.m
//  EverydayNews
//
//  Created by 李沛池 on 2017/7/18.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "ENLoginViewController.h"
// 在需要进行获取登录信息的UIViewController中加入如下代码
#import <UMSocialCore/UMSocialCore.h>
#import <BotBrainPods/BotBrainManager.h>
#import "UserModel.h"

#define TAG_Wechat 521
#define TAG_QQ 522
#define TAG_Sina 523

@interface ENLoginViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cstLogoLeftWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cstLogoRightWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cstLogoLeftSecond;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cstLogoRightSecond;

@end

@implementation ENLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

}

- (void)initUI
{
    _cstLogoLeftWidth.constant = (SCREEN_WIDTH - 55 * 3)/4;
    _cstLogoLeftSecond.constant = _cstLogoLeftWidth.constant;
    _cstLogoRightWidth.constant = _cstLogoLeftWidth.constant;
    _cstLogoRightSecond.constant = _cstLogoLeftWidth.constant;
}

- (IBAction)loginFromThird:(UITapGestureRecognizer *)sender
{
    switch (sender.view.tag) {
        case TAG_Wechat:
        {
            [self getAuthWithUserInfoFromWechat];
        }
            break;
        case TAG_QQ:
        {
            [self getAuthWithUserInfoFromQQ];
        }
            break;
        case TAG_Sina:
        {
            [self getAuthWithUserInfoFromSina];
        }
            break;
        default:
            break;
    }
}

- (void)loginSuccess:(UMSocialUserInfoResponse *)resp
{
    //保存用户登录信息
    [UserModel saveUserData:resp];
    //登录布本智能
    [self logInCommentSystem:resp];
    //返回上级菜单
    PopVC;
}

- (void)logInCommentSystem:(UMSocialUserInfoResponse *)resp
{
    BotBrainUserInfoModel *userInfo = [BotBrainUserInfoModel new];
    userInfo.userID = resp.uid;
    userInfo.userIcon = resp.iconurl;
    userInfo.userNickName = resp.name;
    [[BotBrainManager defaultManager] loginWithUserInfo:userInfo success:^(BotBrainUserInfoModel *userInfo) {
        NSLog(@"UserInfo：%@", userInfo);
    } failure:^(NSError *error) {
        NSLog(@"Error：%@", error);
    }];
}

- (void)getAuthWithUserInfoFromSina
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Sina uid: %@", resp.uid);
            NSLog(@"Sina accessToken: %@", resp.accessToken);
            NSLog(@"Sina refreshToken: %@", resp.refreshToken);
            NSLog(@"Sina expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Sina name: %@", resp.name);
            NSLog(@"Sina iconurl: %@", resp.iconurl);
            NSLog(@"Sina gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"Sina originalResponse: %@", resp.originalResponse);
            
            [self loginSuccess:resp];
        }
    }];
}

- (void)getAuthWithUserInfoFromQQ
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"QQ uid: %@", resp.uid);
            NSLog(@"QQ openid: %@", resp.openid);
            NSLog(@"QQ unionid: %@", resp.unionId);
            NSLog(@"QQ accessToken: %@", resp.accessToken);
            NSLog(@"QQ expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"QQ name: %@", resp.name);
            NSLog(@"QQ iconurl: %@", resp.iconurl);
            NSLog(@"QQ gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"QQ originalResponse: %@", resp.originalResponse);
            
            [self loginSuccess:resp];
        }
    }];
}

- (void)getAuthWithUserInfoFromWechat
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat unionid: %@", resp.unionId);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
            
            [self loginSuccess:resp];
        }
    }];
}





@end
